package com.dhcc.sdc.busi.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.core.framework.base.service.impl.BaseServiceImpl;
import com.dhcc.sdc.busi.dao.SdcShowPremiseMapper;
import com.dhcc.sdc.busi.entity.SdcShowPremiseEntity;
import com.dhcc.sdc.busi.service.ISdcShowPremiseService;
import com.dhcc.sds.base.entity.SdsQcentityEntity;
import com.dhcc.sds.base.service.ISdsQcentityService;
import com.dhcc.sds.standard.entity.SdsStandSourceEntity;
import com.dhcc.sds.standard.entity.SdsStandSourceFieldEntity;
import com.dhcc.sds.standard.service.ISdsStandSourceFieldService;
import com.dhcc.sds.standard.service.ISdsStandSourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 质控规则表数据服务层接口
 *
 * @ClassName: SdcShowPremiseServiceImpl
 * @Description: TODO
 * @author zhangdc
 * @date 2022-09-26 14:24:20
 */
@Service("sdcShowPremise")
public class SdcShowPremiseServiceImpl extends BaseServiceImpl<SdcShowPremiseMapper, SdcShowPremiseEntity> implements ISdcShowPremiseService {

    @Autowired
    private ISdsStandSourceService standSourceService;
    @Autowired
    private ISdsStandSourceFieldService sdsStandSourceFieldService;
    @Autowired
    private ISdsQcentityService sdsQcentityService;

    @Override
    public List<SdcShowPremiseEntity> page(Page<SdcShowPremiseEntity> page, Map<String, Object> map) {
        return this.baseMapper.page(page, map);
    }

    @Override
    public List<SdcShowPremiseEntity> list(Map<String, Object> map) {
        return this.baseMapper.list(map);
    }

    @Override
    public List<SdcShowPremiseEntity> selectByShowPremise(Map<String, Object> params) {
        List<SdcShowPremiseEntity> res = new ArrayList<>();

        String compare = (String) params.get("compare");
        String showPremise = "";
        if (!StringUtils.isEmpty(params.get("showPremise"))){
            try {
                showPremise = java.net.URLDecoder.decode((String) params.get("showPremise"), "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            List<String> list = handleRuleExpressions(showPremise);
            if (Objects.equals("all", compare)){
                String s = list.get(0);
                String[] split = s.split("_");
                params.put("expCode", split[0]);
                res = this.baseMapper.list(params);
            }else {
                res = this.baseMapper.selectByExpressions(list);
            }
        }else {
            String qcentityId = (String) params.get("qcentityId");
            SdsQcentityEntity sdsQcentityEntity = sdsQcentityService.selectById(Long.parseLong(qcentityId));
            params.put("expCode", sdsQcentityEntity.getAbbrev());
            res = this.baseMapper.list(params);
        }

        for (SdcShowPremiseEntity sdcShowPremiseEntity : res){
            String ruleCal = "";
            if (!StringUtils.isEmpty(sdcShowPremiseEntity.getDataSource1()) && !StringUtils.isEmpty(sdcShowPremiseEntity.getDataSourceItem1())){
                SdsStandSourceEntity sourceEntity1 = standSourceService.selectById(sdcShowPremiseEntity.getDataSource1());
                SdsStandSourceFieldEntity sourceFieldEntity1 = sdsStandSourceFieldService.selectById(sdcShowPremiseEntity.getDataSourceItem1());
                ruleCal = sourceEntity1.getSourceDesc() + ":" + sourceFieldEntity1.getFieldDesc();
            }
            if (!StringUtils.isEmpty(sdcShowPremiseEntity.getParam1())){
                ruleCal = ruleCal + "(" + sdcShowPremiseEntity.getParam1() + ")";
            }
            if (!StringUtils.isEmpty(sdcShowPremiseEntity.getOperator1())){
                ruleCal = ruleCal + sdcShowPremiseEntity.getOperator1();
            }
            if (!StringUtils.isEmpty(sdcShowPremiseEntity.getDataSource2()) && !StringUtils.isEmpty(sdcShowPremiseEntity.getDataSourceItem2())){
                SdsStandSourceEntity sourceEntity2 = standSourceService.selectById(sdcShowPremiseEntity.getDataSource2());
                SdsStandSourceFieldEntity sourceFieldEntity2 = sdsStandSourceFieldService.selectById(sdcShowPremiseEntity.getDataSourceItem2());
                ruleCal = ruleCal + sourceEntity2.getSourceDesc() + ":" + sourceFieldEntity2.getFieldDesc();
            }
            if (!StringUtils.isEmpty(sdcShowPremiseEntity.getParam2())){
                ruleCal = ruleCal + "(" + sdcShowPremiseEntity.getParam2() + ")";
            }
            if (!StringUtils.isEmpty(sdcShowPremiseEntity.getOperator2())){
                ruleCal = ruleCal + sdcShowPremiseEntity.getOperator2();
            }
            sdcShowPremiseEntity.setRuleCal(ruleCal);
        }
        return res;
    }

    private List<String> handleRuleExpressions(String showPremise) {
        //小括号及其里面内容
        String p1 = "\\(.*?\\)";
        List<String> res = new ArrayList<>();
        Pattern p = Pattern.compile(p1);
        Matcher m = p.matcher(showPremise);
        while (m.find()){
            String ruleExpression = m.group().substring(1, m.group().length() - 1);
            res.add(ruleExpression);
        }
        return res;
    }
}
